package arcane.object.gui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import org.lwjgl.opengl.GL11;

import arcane.constant.Constants;

import com.golden.gamedev.engine.BaseInput;
import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.util.ImageUtil;

public class DefaultTextField extends DefaultLabel {
	protected int insertionPoint;
	private int visiblePos;
	protected Color bgColor;
	/**
	 * Indicates whether to draw cursor. Cursor pulses as in traditional
	 * applications.
	 */
	private boolean displayCaret;
	private boolean capsLock;

	private long tick;
	private static final char[] CAPS_NUMERIC = { '!', '@', '#', '$', '%', '^',
			'&', '*', '(', ')' };

	BufferedImage textUI;

	public DefaultTextField(int x, int y, int columns) {
		super("");
		insertionPoint = 0;
		setLocation(x, y);
		bgColor = Color.WHITE;
		GameFont font = getFont();
		height = font.getHeight() + 6;
		width = font.getWidth("ABCDEFGHIJKLMNOPQRSTUVWXYZ") / 26 * columns;
		displayCaret = capsLock = false;
		createTextUI();
		color=Color.BLACK;
	}

	protected void processKeyEvent() {
		int key = Constants.keyStroke.getKeyReleased();
		boolean caps = false;
		if (Constants.keyStroke.getKeyPressed() == KeyEvent.VK_SHIFT) {
			caps = true;
		}
		if (key != BaseInput.NO_KEY) {
			switch (key) {
			case KeyEvent.VK_LEFT:
				break;
			case KeyEvent.VK_RIGHT:
				break;
			case KeyEvent.VK_SHIFT:
				break;
			case KeyEvent.VK_CAPS_LOCK:
				break;
			default:
				if (key >= '0' && key <= '9') {
					// if()
				} else {
				}
				;
			}
		}
	}

	public void createTextUI() {
		textUI = ImageUtil.createImage(getWidth(), getHeight(),
				Transparency.TRANSLUCENT);
		Graphics2D g = textUI.createGraphics();
		g.setClip(5, 2, getWidth() - 10, getHeight() - 4);
		g.setColor((isEnabled()) ? Color.BLACK : new Color(172, 168, 153));
		setOpenGLColor(color);
		getFont().drawString(g, text, 0,
				getHeight() / 2 - getFont().getHeight() / 2);
		System.out.println((12-visiblePos));
		g.dispose();
	}

	public void render(Graphics2D g, int x, int y) {
		if (isActive()) {
			g.setColor(Color.BLACK);
			g.drawRect(x, y, width, height);
			g.setColor(bgColor);
			g.fillRect(x + 1, y + 1, width - 2, height - 2);
			drawText(g, x, y);
		}
	}

	public void drawText(Graphics2D g, int x, int y) {
		g.drawImage(textUI, x, y, null);
	}

	public void update(long time) {
		if (System.currentTimeMillis() - tick > 500) {
			this.displayCaret = !displayCaret;
		}
		// Some key was pressed
		int key = Constants.keyStroke.getKeyReleased();
		if (key != BaseInput.NO_KEY) {
			/*
			 * switch (key) { case KeyEvent.VK_DELETE: // Not at end of list
			 * field if (insertionPoint != text.length()) { if (insertionPoint
			 * == text.length() - 1) text = text.substring(0, insertionPoint);
			 * else text = text.substring(0, insertionPoint) +
			 * text.substring(insertionPoint); } break; case
			 * KeyEvent.VK_BACK_SPACE: if (insertionPoint > 0) { if
			 * (insertionPoint == 1) text = text.substring(insertionPoint - 1);
			 * else text = text.substring(0, insertionPoint - 1) +
			 * text.substring(insertionPoint + 1); } break; case
			 * KeyEvent.VK_LEFT: insertionPoint = --insertionPoint < 0 ? 0 :
			 * insertionPoint; break; case KeyEvent.VK_RIGHT: insertionPoint =
			 * ++insertionPoint > text.length() ? --insertionPoint :
			 * insertionPoint; break; default: text +=
			 * String.valueOf(KeyEvent.getKeyText(key)); }
			 */
			String s = KeyEvent.getKeyText(key);
			text += String.valueOf(s);
			visiblePos+=getFont().getWidth(s);
			createTextUI();
		}
	}
}
